package com.tianpengtech.common.search;

import java.util.ArrayList;
import java.util.List;

public class QueryConstructor {

    StringBuffer sqlBuffer = new StringBuffer();
    String sqlCommand = "select * from t_user";
    String orderSql = " order by id desc";

    public String getOrderSql() {
        return orderSql;
    }

    public void setOrderSql(String orderSql) {
        this.orderSql = orderSql;
    }

    List<Condition> AndConditions = new ArrayList<Condition>();
    List<Condition> OrConditions = new ArrayList<Condition>();
    Condition condition;

    List<Object> params = new ArrayList<Object>();

    public List<Object> getParams() {
        return params;
    }

    public void setParams(List<Object> params) {
        this.params = params;
    }

    public QueryConstructor setCommand(String value) {
        this.sqlCommand = value;
        return this;
    }

    public String buildSQL() {

        String conditionSQL = "";
        if (condition != null) {
            conditionSQL = condition.buildSQL();
        }
        String andSQL = formatAndSQL();
        String orSQL = formatOrSQL();
        String sql = isContainsWhere(sqlCommand) + conditionSQL + andSQL + orSQL + orderSql;
        return sql.toLowerCase();
    }

    public String buildSQL(String orderSql) {

        String conditionSQL = "";
        if (condition != null) {
            conditionSQL = condition.buildSQL();
        }
        String andSQL = formatAndSQL();
        String orSQL = formatOrSQL();
        String sql = isContainsWhere(sqlCommand) + conditionSQL + andSQL + orSQL + orderSql;
        return sql.toLowerCase();
    }

    private String isContainsWhere(String command) {
        command = command.toLowerCase();

        if (params.size() > 0 && !command.contains("where")) {
            command = command + " where ";
        }
        return command;
    }

    private String formatAndSQL() {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        for (Condition condition : AndConditions) {
            String result = condition.buildSQL();
            if (i == 0) {
                sb.append(result);
            } else {
                sb.append(" and ").append(result);
            }
            i = i + 1;
        }
        String expectedSQL = sb.toString();
        return expectedSQL;
    }

    private String formatOrSQL() {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        for (Condition condition : OrConditions) {
            String result = condition.buildSQL();
            if (i == 0) {
                sb.append(result);
            } else {
                sb.append(" or ").append(result);
            }
            i = i + 1;
        }
        String expectedSQL = sb.toString();
        return expectedSQL;
    }

    @Deprecated
    public QueryConstructor add(Condition condition) {
        this.condition = condition;
        params.add(condition.getValue());
        return this;
    }

    public QueryConstructor and(Condition condition) {
        this.AndConditions.add(condition);
        params.add(condition.getValue());
        return this;
    }

    public QueryConstructor or(Condition condition) {
        this.OrConditions.add(condition);
        params.add(condition.getValue());
        return this;
    }

}
